package cn.edu.heuet.bnve.activity.SettingActivity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.databinding.DataBindingUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import java.text.SimpleDateFormat;
import java.util.Date;

import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.databinding.ActivityTableSettingBinding;
import cn.edu.heuet.bnve.utils.DialogUtil;
import cn.edu.heuet.bnve.utils.SharepreferencesUtilSystemSettings;

public class TableSettingActivity extends AppCompatActivity {

    private ActivityTableSettingBinding binding;
    private XUIGroupListView mGroupListView;
    private Toolbar toolbar;

    private TimePickerView mDatePicker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_table_setting);
        initData();
        initView();
        initEvent();
    }

    public static void show(Context context) {
        context.startActivity(new Intent(context, TableSettingActivity.class));
    }


    public void initData(){

    }

    public void initView(){
        mGroupListView = binding.tableGroupListView;
        initGroupListView();

        toolbar=binding.toolbarContainer.toolbar;
        setSupportActionBar(toolbar);
        toolbar.setTitle(R.string.setting);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == android.R.id.home)
        {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void initEvent(){


    }
    private void initGroupListView() {

        XUICommonListItemView item1 = mGroupListView.createItemView(
                "学期起始日");
        item1.setTag(1);
        item1.setOrientation(XUICommonListItemView.HORIZONTAL);
        item1.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        item1.setDetailText(SharepreferencesUtilSystemSettings.getValue(this,"table_start_date","无"));

        XUICommonListItemView item2 = mGroupListView.createItemView(
                "学期名称");
        item2.setTag(2);
        item2.setOrientation(XUICommonListItemView.HORIZONTAL);
        item2.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
        item2.setDetailText(SharepreferencesUtilSystemSettings.getValue(this,"table_name","学期"));

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof XUICommonListItemView) {
                    int tagValue = (Integer) ((XUICommonListItemView) v).getTag();
                    switch (tagValue){
                        case 1:
                            showDatePicker(item1);
                            break;
                        case 2:
                            DialogUtil.showInputDialog(TableSettingActivity.this,"请输入学期名称",item2,10,"table_name");
                            break;
                        default:
                            break;
                    }

                }
            }
        };

        XUIGroupListView.newSection(this)
                .setTitle("课程表设置")
                .addItemView(item1, onClickListener)
                .addItemView(item2, onClickListener)
                .addTo(mGroupListView);

    }

    private void showDatePicker(XUICommonListItemView itemView) {
        if (mDatePicker == null) {
            mDatePicker = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    itemView.setDetailText(sdf.format(date));
                    SharepreferencesUtilSystemSettings.putValue(TableSettingActivity.this,"table_start_date",sdf.format(date));
                }
            })
                    .setTimeSelectChangeListener(date -> Log.i("pvTime", "onTimeSelectChanged"))
                    .setTitleText("日期选择")
                    .build();
        }
        mDatePicker.show();
    }
}